package com.example.rdb.handle.redis;

import com.example.rdb.conf.ConfigInfo;
import com.example.rdb.conf.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author XYC
 * @描述: TODO
 * @date 21.5.2817:32
 */
@Component
public class LgetTest {

    @Autowired
    @Qualifier(value = "cacheRedisTemplate")
    private RedisTemplate<String, Object> defaultTemplate;


//    @PostConstruct
    public void test() {
        Map newmap = updateValueList();

        defaultTemplate.delete(ConfigInfo.tenantRead+":"+ConfigInfo.appRead+":Subassembly:list");
        defaultTemplate.opsForHash().putAll(ConfigInfo.tenantWrite+":"+ConfigInfo.appWrite+":Subassembly:list",newmap);
    }

    private Map updateValueList() {
        Map map = (Map) defaultTemplate.opsForHash().entries(ConfigInfo.tenantRead+":"+ConfigInfo.appRead+":AdPool:list");
        //List<Object> aihao = defaultTemplate.opsForList().range("2:ce65cec8c4e948a0:Subassembly:list", 0, -1);
        Set set = map.keySet();
        Map newmap = new HashMap();
        for (Object key : set) {
            String newKey = changeKey(String.valueOf(key));
            newmap.put(newKey,map.get(key));
        }
        defaultTemplate.delete(ConfigInfo.tenantRead+":"+ConfigInfo.appRead+":AdPool:list");
        defaultTemplate.opsForHash().putAll(ConfigInfo.tenantWrite+":"+ConfigInfo.appWrite+":AdPool:list",newmap);
        return newmap;
    }

    public String changeKey(String key){
        String[] split = key.split(":");
        split[0] = ConfigInfo.tenantWrite;
        split[1] = ConfigInfo.appWrite;
        return String.join(":", split);
    }
}
